﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.Security.Cryptography;
using System.IO;
using Org.BouncyCastle.Crypto;
using System.Reflection;
using System.Windows.Markup;

namespace Aes256GcmExample
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private string filePath = string.Empty;
        private void Select_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Multiselect = true; // 允许选择多个文件
            openFileDialog.Filter = "所有文件 (*.*)|*.*"; // 设置文件过滤器，根据需要调整
            var res = openFileDialog.ShowDialog();
            if (res.Value)
            {
                filePath = openFileDialog.FileName;
                txtFilePath.Dispatcher.Invoke(() =>
                {
                    txtFilePath.Text = System.IO.Path.GetDirectoryName(openFileDialog.FileName);
                });
            }
        }

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Encryption_Click(object sender, RoutedEventArgs e)
        {
            // 读取文本的秘钥
            var key = File.ReadAllText(filePath);
            this.txtRes.Text = Aes_GCM_Helper.Encrypt(txtPwd.Text.Trim(), key);
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Decrypt_Click(object sender, RoutedEventArgs e)
        {
            var key = File.ReadAllText(filePath);
            this.txtRes.Text = Aes_GCM_Helper.Decrypt(txtPwd.Text.Trim(), key);
        }
    }
}
